package com.ly.oa.action.flow;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ly.core.command.QueryFilter;
import com.ly.core.util.ContextUtil;
import com.ly.core.web.action.BaseAction;
import com.ly.oa.model.flow.ProcessForm;
import com.ly.oa.model.flow.ProcessRun;
import com.ly.oa.service.flow.FormDataService;
import com.ly.oa.service.flow.ProcessFormService;
import com.ly.oa.service.flow.ProcessRunService;

/**
 * 
 * @author csx
 * 
 */
public class ProcessRunAction extends BaseAction {
	@Resource
	private ProcessRunService processRunService;
	@Resource
	private ProcessFormService processFormService;
	@Resource
	private FormDataService formDataService;
	
	private ProcessRun processRun;

	private Long runId;

	public Long getRunId() {
		return runId;
	}

	public void setRunId(Long runId) {
		this.runId = runId;
	}

	public ProcessRun getProcessRun() {
		return processRun;
	}

	public void setProcessRun(ProcessRun processRun) {
		this.processRun = processRun;
	}

	/**
	 * 显示列表
	 */
	public String list() {

		QueryFilter filter = new QueryFilter(getRequest());

		// 加上过滤条件，表示只显示当前用户的申请
		filter.addFilter("Q_appUser.userId_L_EQ", ContextUtil
				.getCurrentUserId().toString());
		List<ProcessRun> list = processRunService.getAll(filter);
		Type type = new TypeToken<List<ProcessRun>>() {
		}.getType();
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
				.append(filter.getPagingBean().getTotalItems()).append(
						",result:[");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (ProcessRun run : list) {
			buff.append("{runId:'").append(run.getRunId())
					.append("',runStatus:'").append(run.getRunStatus())
					.append("',subject:'").append(run.getSubject()).append(
							"',createtime:'").append(
							sdf.format(run.getCreatetime())).append("',piId:'")
					.append(run.getPiId()).append("',defId:'").append(
							run.getProDefinition().getDefId()).append("'},");
		}

		if (list.size() > 0) {
			buff.deleteCharAt(buff.length() - 1);
		}
		buff.append("]");
		buff.append("}");
		System.out.println(buff.toString());
		jsonString = buff.toString();

		return SUCCESS;
	}

	/**
	 * 批量删除
	 * 
	 * @return
	 */
	public String multiDel() {

		String[] ids = getRequest().getParameterValues("ids");
		if (ids != null) {
			for (String id : ids) {
				List formlist=processFormService.getByRunId(new Long(id));
				for(ProcessForm v:(List<ProcessForm>)formlist){
					formDataService.removeObjs(formDataService.getByFormId(v.getFormId()));
				}
				processFormService.removeObjs(formlist);
				processRunService.remove(new Long(id));
			}
		}

		jsonString = "{success:true}";

		return SUCCESS;
	}

	/**
	 * 显示详细信息
	 * 
	 * @return
	 */
	public String get() {
		ProcessRun processRun = processRunService.get(runId);

		Gson gson = new Gson();
		// 将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(processRun));
		sb.append("}");
		setJsonString(sb.toString());

		return SUCCESS;
	}

	/**
	 * 添加及保存操作
	 */
	public String save() {
		processRunService.save(processRun);
		setJsonString("{success:true}");
		return SUCCESS;
	}

	public String my() {
		QueryFilter filter = new QueryFilter(getRequest());

		filter.setFilterName("MyAttendProcessRun");

		filter.addParamValue(ContextUtil.getCurrentUserId());

		List<ProcessRun> processRunList = this.processRunService.getAll(filter);

		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
				.append(filter.getPagingBean().getTotalItems()).append(
						",result:[");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		for (ProcessRun run : processRunList) {
			buff.append("{runId:'").append(run.getRunId())
					.append("',subject:'").append(run.getSubject()).append(
							"',createtime:'").append(
							sdf.format(run.getCreatetime())).append("',piId:'")
					.append(run.getPiId()).append("',defId:'").append(
							run.getProDefinition().getDefId()).append(
							"',runStatus:'").append(run.getRunStatus()).append(
							"'},");
		}

		if (processRunList.size() > 0) {
			buff.deleteCharAt(buff.length() - 1);
		}
		buff.append("]");
		buff.append("}");

		this.jsonString = buff.toString();
		return "success";
	}
}
